package com.wheel.autoconfigure.web.mvc.argumentresolver;/*
 * <h3>Title:bs-faceidservice2.0系统</h3>
 * <p>Copyright (c) 2020, wangnaifei@163.com. All rights reserved.</p>
 * @author wangnaifei@163.com
 */

import com.wheel.core.exception.GlobalAppException;
import com.wheel.core.utils.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.MethodParameter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <dt>Description:TokenArgumentResolver</dt>
 * <dd>把请求中的head中的参数，分装的方法参数中</dd>
 *
 * @author wangnaifei@163.com
 * @version 1.0 <i>2020/10/28 10:01</i>
 * @see org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor  参考
 * @since jdk1.7+
 */
public abstract class BinderRequestResponseBodyMethodProcessor extends RequestResponseBodyMethodProcessor {

    private static final Logger log = LoggerFactory.getLogger(BinderRequestResponseBodyMethodProcessor.class);

    public BinderRequestResponseBodyMethodProcessor(List<HttpMessageConverter<?>> converters) {
        super(converters);
        Log.registered(log);
    }

    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        return methodParameter.hasParameterAnnotation(RequestBody.class);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        Object arg = super.resolveArgument(parameter, mavContainer, webRequest, binderFactory);
        HttpServletRequest httpServletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
        doBind(parameter, arg, httpServletRequest);
        return arg;
    }

    public abstract void doBind(MethodParameter parameter, Object paramObject, HttpServletRequest request) throws GlobalAppException;
}
